

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/boshi.jpeg">
  <link rel="icon" type="image/png" href="/img/boshi.jpeg">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="">
  <meta name="author" content="wangpf">
  <meta name="keywords" content="">
  <title>TypeScript 学习总结之 类 - wpf的博客</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/foundation.min.css" />
  

  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_6peoq002giu.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_pjno9b9zyxs.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.1.1"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>Wangpf's blog</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner intro-2" id="background" parallax=true
         style="background: url('/img/article.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container page-header text-center fade-in-up">
            <span class="h2" id="subtitle">
              
            </span>

            
              
  <div class="mt-3 post-meta">
    <i class="iconfont icon-date-fill" aria-hidden="true"></i>
    <time datetime="2020-12-29 21:05" pubdate>
      2020年12月29日 晚上
    </time>
  </div>


<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      1.2k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      19
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto" id="post">
            <!-- SEO header -->
            <h1 style="display: none">TypeScript 学习总结之 类</h1>
            
            <div class="markdown-body" id="post-body">
              <p>​<br><img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/408cfbc308734bd8bcbb8a36c40135d0~tplv-k3u1fbpfcp-watermark.image" srcset="/img/loading.gif" alt="TypeScript"></p>
<h2 id="类的注解方式"><a href="#类的注解方式" class="headerlink" title="类的注解方式"></a>类的注解方式</h2><div class="hljs"><pre><code class="hljs ts"><span class="hljs-keyword">class</span> Animal &#123;
  name: <span class="hljs-built_in">string</span>;
  <span class="hljs-keyword">constructor</span>(<span class="hljs-params">theName: <span class="hljs-built_in">string</span></span>) &#123; <span class="hljs-built_in">this</span>.name = theName; &#125;
  move(distanceInMeters: <span class="hljs-built_in">number</span> = <span class="hljs-number">0</span>) &#123;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">$&#123;<span class="hljs-built_in">this</span>.name&#125;</span> moved <span class="hljs-subst">$&#123;distanceInMeters&#125;</span>m.`</span>);
  &#125;
&#125;</code></pre></div>

<h3 id="继承中super"><a href="#继承中super" class="headerlink" title="继承中super"></a>继承中super</h3><p><strong>// 注意这个super， 如果在constructor内就是默认指 构造函数 。</strong></p>
<p><strong>// 如果在constructor外部（除了constructor），这个super 就是指的 父类 （Animal）。</strong></p>
<div class="hljs"><pre><code class="hljs ts"><span class="hljs-keyword">class</span> Snake <span class="hljs-keyword">extends</span> Animal &#123;
  <span class="hljs-keyword">constructor</span>(<span class="hljs-params">name: <span class="hljs-built_in">string</span></span>) &#123; <span class="hljs-built_in">super</span>(name); &#125;  <span class="hljs-comment">// 这里的super  =&gt; constructor Animal(theName: string) : Animal</span>
  move(distanceInMeters = <span class="hljs-number">5</span>) &#123;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;Slithering...&quot;</span>);
    <span class="hljs-built_in">super</span>.move(distanceInMeters);  <span class="hljs-comment">// 这里的 super =&gt; 父类 Animal</span>
  &#125;
&#125;</code></pre></div>

<h2 id="类的修饰符"><a href="#类的修饰符" class="headerlink" title="类的修饰符"></a>类的修饰符</h2><blockquote>
<p>三大类成员修饰符: public / private  / protected</p>
</blockquote>
<h3 id="public"><a href="#public" class="headerlink" title="public"></a>public</h3><p>// 在TypeScript里，成员都默认为 public。</p>
<p>// 如果类默认不写, 就是public </p>
<p><strong>public : 公共成员属性</strong></p>
<ul>
<li>自身类可以调用</li>
<li>子类可以调用</li>
<li>实例可以调用</li>
</ul>
<div class="hljs"><pre><code class="hljs ts"><span class="hljs-keyword">class</span> Animal &#123;
  <span class="hljs-keyword">public</span> name: <span class="hljs-built_in">string</span>;
  <span class="hljs-keyword">public</span> <span class="hljs-keyword">constructor</span>(<span class="hljs-params">theName: <span class="hljs-built_in">string</span></span>) &#123; <span class="hljs-built_in">this</span>.name = theName; &#125;
  <span class="hljs-keyword">public</span> move(distanceInMeters: <span class="hljs-built_in">number</span>) &#123;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">$&#123;<span class="hljs-built_in">this</span>.name&#125;</span> moved <span class="hljs-subst">$&#123;distanceInMeters&#125;</span>m.`</span>);
  &#125;
&#125;</code></pre></div>

<h3 id="private"><a href="#private" class="headerlink" title="private"></a>private</h3><p>// private : 私有属性</p>
<ul>
<li>只能自身调用</li>
</ul>
<p>// 当我把 Animal 类中的 构造函数变成私有, 如下:</p>
<div class="hljs"><pre><code class="hljs ts"><span class="hljs-keyword">class</span> Animal &#123;
  <span class="hljs-keyword">public</span> name: <span class="hljs-built_in">string</span>;
  <span class="hljs-keyword">private</span> <span class="hljs-keyword">constructor</span>(<span class="hljs-params">theName: <span class="hljs-built_in">string</span></span>) &#123; <span class="hljs-built_in">this</span>.name = theName; &#125;
  <span class="hljs-keyword">public</span> move(distanceInMeters: <span class="hljs-built_in">number</span> = <span class="hljs-number">0</span>) &#123;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">$&#123;<span class="hljs-built_in">this</span>.name&#125;</span> moved <span class="hljs-subst">$&#123;distanceInMeters&#125;</span>m.`</span>);
  &#125;
&#125;

<span class="hljs-keyword">class</span> Snake <span class="hljs-keyword">extends</span> Animal &#123;
  <span class="hljs-keyword">constructor</span>(<span class="hljs-params">name: <span class="hljs-built_in">string</span></span>) &#123; <span class="hljs-built_in">super</span>(name); &#125;
  move(distanceInMeters = <span class="hljs-number">5</span>) &#123;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;Slithering...&quot;</span>);
    <span class="hljs-built_in">super</span>.move(distanceInMeters);
  &#125;
&#125;

<span class="hljs-keyword">let</span> sam = <span class="hljs-keyword">new</span> Snake(<span class="hljs-string">&quot;Sammy the Python&quot;</span>);
sam.move();</code></pre></div>
<p>当我把 构造函数变成私有属性时, 编辑器会提示说  我子类(Snake)继承不了<br><img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/13dfba4196e34680b9a27f352ae5cc92~tplv-k3u1fbpfcp-watermark.image" srcset="/img/loading.gif" alt="private"></p>
<p>// 当我把 Animal 中的 move 方法设为 private 时, 会出现如下报错:<br><img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/72ed13d2a70c4aa780b40af21f168732~tplv-k3u1fbpfcp-watermark.image" srcset="/img/loading.gif" alt="private"></p>
<p><strong>因此 , 可证明  private  只能自身调用,  子类和实例不能调用.</strong></p>
<h3 id="protected"><a href="#protected" class="headerlink" title="protected"></a>protected</h3><blockquote>
<p>protected修饰符与 private修饰符的行为很相似，但有一点不同， protected成员在派生类中仍然可以访问。</p>
</blockquote>
<p>也就是说 protected 可以自身调用,也可以子类调用.</p>
<div class="hljs"><pre><code class="hljs ts"><span class="hljs-keyword">class</span> Animal &#123;
  <span class="hljs-keyword">public</span> name: <span class="hljs-built_in">string</span>;
  <span class="hljs-keyword">public</span> <span class="hljs-keyword">constructor</span>(<span class="hljs-params">theName: <span class="hljs-built_in">string</span></span>) &#123; <span class="hljs-built_in">this</span>.name = theName; &#125;
  <span class="hljs-keyword">protected</span> move(distanceInMeters: <span class="hljs-built_in">number</span> = <span class="hljs-number">0</span>) &#123;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">$&#123;<span class="hljs-built_in">this</span>.name&#125;</span> moved <span class="hljs-subst">$&#123;distanceInMeters&#125;</span>m.`</span>);
  &#125;
&#125;

<span class="hljs-keyword">class</span> Snake <span class="hljs-keyword">extends</span> Animal &#123;
  <span class="hljs-keyword">constructor</span>(<span class="hljs-params">name: <span class="hljs-built_in">string</span></span>) &#123; <span class="hljs-built_in">super</span>(name); &#125;
  move(distanceInMeters = <span class="hljs-number">5</span>) &#123;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;Slithering...&quot;</span>);
    <span class="hljs-built_in">super</span>.move(distanceInMeters);
  &#125;
&#125;

<span class="hljs-keyword">let</span> jack = <span class="hljs-keyword">new</span> Animal(<span class="hljs-string">&#x27;jack&#x27;</span>)
jack.move()   <span class="hljs-comment">// 报错,</span></code></pre></div>

<p>这时,在编辑器上, 实例调用move方法会报错,<br><img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/7baff8b10ad1472189dc44c3a19325ec~tplv-k3u1fbpfcp-watermark.image" srcset="/img/loading.gif" alt="protected"> </p>
<h3 id="readonly-只读-修饰符"><a href="#readonly-只读-修饰符" class="headerlink" title="readonly(只读)修饰符"></a>readonly(只读)修饰符</h3><div class="hljs"><pre><code class="hljs ts"><span class="hljs-keyword">class</span> Octopus &#123;
  <span class="hljs-keyword">public</span> <span class="hljs-keyword">readonly</span> name: <span class="hljs-built_in">string</span>;   <span class="hljs-comment">// readonly 要放在 public 这种访问属性修饰符 后面</span>
  <span class="hljs-keyword">private</span> <span class="hljs-keyword">readonly</span> numberOfLegs: <span class="hljs-built_in">number</span> = <span class="hljs-number">8</span>;
  <span class="hljs-keyword">constructor</span>(<span class="hljs-params">theName: <span class="hljs-built_in">string</span></span>) &#123;   
    <span class="hljs-built_in">this</span>.name = theName;
  &#125;
  <span class="hljs-keyword">readonly</span> say() &#123;   <span class="hljs-comment">// 错误 : 修饰符仅可出现在属性声明或索引签名中</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;hi&#x27;</span>);
  &#125;
&#125;
<span class="hljs-keyword">let</span> dad = <span class="hljs-keyword">new</span> Octopus(<span class="hljs-string">&quot;Man with the 8 strong legs&quot;</span>);
dad.name = <span class="hljs-string">&quot;Man with the 3-piece suit&quot;</span>; <span class="hljs-comment">// 错误! name 是只读的.</span></code></pre></div>

<p>readonly 注意事项:</p>
<ul>
<li><strong>顺序:  要放在 public/private/protected  之后</strong></li>
<li><strong>是否可写: 加上 readonly  表示 只读,不可写</strong></li>
<li><strong>不能修饰成员方法.</strong></li>
</ul>
<p><img src="https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5cb3ac948e30435391cd2b9cd516036b~tplv-k3u1fbpfcp-watermark.image" srcset="/img/loading.gif" alt="readonly"></p>
<h3 id="参数属性"><a href="#参数属性" class="headerlink" title="参数属性"></a>参数属性</h3><blockquote>
<p>简写写法</p>
</blockquote>
<div class="hljs"><pre><code class="hljs ts"><span class="hljs-keyword">class</span> Animal1 &#123;
  <span class="hljs-keyword">constructor</span>(<span class="hljs-params"><span class="hljs-keyword">private</span> name: <span class="hljs-built_in">string</span></span>) &#123; &#125;
  move(distanceInMeters: <span class="hljs-built_in">number</span> = <span class="hljs-number">0</span>) &#123;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">$&#123;<span class="hljs-built_in">this</span>.name&#125;</span> moved <span class="hljs-subst">$&#123;distanceInMeters&#125;</span>m.`</span>);
  &#125;
&#125;

<span class="hljs-comment">// 等价于</span>

<span class="hljs-keyword">class</span> Animal2 &#123;
  <span class="hljs-keyword">private</span> name: <span class="hljs-built_in">string</span>
  <span class="hljs-keyword">constructor</span>(<span class="hljs-params">name: <span class="hljs-built_in">string</span></span>) &#123; <span class="hljs-built_in">this</span>.name = name &#125;
  move(distanceInMeters: <span class="hljs-built_in">number</span> = <span class="hljs-number">0</span>) &#123;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">$&#123;<span class="hljs-built_in">this</span>.name&#125;</span> moved <span class="hljs-subst">$&#123;distanceInMeters&#125;</span>m.`</span>);
  &#125;
&#125;</code></pre></div>

<h3 id="存取器"><a href="#存取器" class="headerlink" title="存取器"></a>存取器</h3><blockquote>
<p>大概看了一下,  其实就是 getter 和 setter</p>
</blockquote>
<div class="hljs"><pre><code class="hljs ts"><span class="hljs-keyword">let</span> passcode = <span class="hljs-string">&quot;secret passcode&quot;</span>;

<span class="hljs-keyword">class</span> Employee &#123;
  <span class="hljs-keyword">private</span> _fullName: <span class="hljs-built_in">string</span> = <span class="hljs-string">&#x27;jack&#x27;</span>;

  get fullName(): <span class="hljs-built_in">string</span> &#123;
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">this</span>._fullName;
  &#125;

  set fullName(newName: <span class="hljs-built_in">string</span>) &#123;
    <span class="hljs-keyword">if</span> (passcode &amp;&amp; passcode == <span class="hljs-string">&quot;secret passcode&quot;</span>) &#123;
      <span class="hljs-built_in">this</span>._fullName = newName;
    &#125;
    <span class="hljs-keyword">else</span> &#123;
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;Error: Unauthorized update of employee!&quot;</span>);
    &#125;
  &#125;
&#125;

<span class="hljs-keyword">let</span> employee = <span class="hljs-keyword">new</span> Employee();
<span class="hljs-built_in">console</span>.log(employee.fullName);  <span class="hljs-comment">// jack</span>

<span class="hljs-built_in">console</span>.log(employee.fullName = <span class="hljs-string">&#x27;new jack&#x27;</span>);  <span class="hljs-comment">// new jack</span></code></pre></div>


<h3 id="静态属性"><a href="#静态属性" class="headerlink" title="静态属性"></a>静态属性</h3><blockquote>
<p>这个也是 不属于 TS 新有的知识,   学到这里,我发现其实我们大都是还是在复习JS, 要学好TS前提是JS基础一定要好啊</p>
</blockquote>
<div class="hljs"><pre><code class="hljs ts"><span class="hljs-keyword">class</span> Grid &#123;
    <span class="hljs-keyword">static</span> origin = &#123;x: <span class="hljs-number">0</span>, y: <span class="hljs-number">0</span>&#125;;
    calculateDistanceFromOrigin(point: &#123;x: <span class="hljs-built_in">number</span>; y: <span class="hljs-built_in">number</span>;&#125;) &#123;
        <span class="hljs-keyword">let</span> xDist = (point.x - Grid.origin.x);
        <span class="hljs-keyword">let</span> yDist = (point.y - Grid.origin.y);
        <span class="hljs-keyword">return</span> <span class="hljs-built_in">Math</span>.sqrt(xDist * xDist + yDist * yDist) / <span class="hljs-built_in">this</span>.scale;
    &#125;
    <span class="hljs-keyword">constructor</span> (<span class="hljs-params"><span class="hljs-keyword">public</span> scale: <span class="hljs-built_in">number</span></span>) &#123; &#125;
&#125;

<span class="hljs-keyword">let</span> grid1 = <span class="hljs-keyword">new</span> Grid(<span class="hljs-number">1.0</span>);  <span class="hljs-comment">// 1x scale</span>
<span class="hljs-keyword">let</span> grid2 = <span class="hljs-keyword">new</span> Grid(<span class="hljs-number">5.0</span>);  <span class="hljs-comment">// 5x scale</span>

<span class="hljs-built_in">console</span>.log(grid1.calculateDistanceFromOrigin(&#123;x: <span class="hljs-number">10</span>, y: <span class="hljs-number">10</span>&#125;));
<span class="hljs-built_in">console</span>.log(grid2.calculateDistanceFromOrigin(&#123;x: <span class="hljs-number">10</span>, y: <span class="hljs-number">10</span>&#125;));</code></pre></div>

<h3 id="抽象类"><a href="#抽象类" class="headerlink" title="抽象类"></a>抽象类</h3><blockquote>
<p>抽象类: 能够提供其他类的基类  , 它们一般不会直接被实例化</p>
</blockquote>
<p>抽象类:</p>
<ol>
<li>无法创建实例</li>
<li>在抽象类中 抽象方法一定要实现  (这一点和接口很像)</li>
</ol>
<div class="hljs"><pre><code class="hljs ts"><span class="hljs-keyword">abstract</span> <span class="hljs-keyword">class</span> Animal &#123;
  <span class="hljs-keyword">abstract</span> makeSound(): <span class="hljs-built_in">void</span>;
  move(): <span class="hljs-built_in">void</span> &#123;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;我在移动&#x27;</span>);
  &#125;
&#125;

<span class="hljs-keyword">class</span> Dog <span class="hljs-keyword">extends</span> Animal &#123;
  makeSound() &#123;   <span class="hljs-comment">// 一定要去定义这个方法，  因为抽象类中包含这个抽象方法</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;汪汪汪&#x27;</span>);
  &#125;
&#125;

<span class="hljs-keyword">class</span> Cat <span class="hljs-keyword">extends</span> Animal &#123;
  makeSound() &#123;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&#x27;喵喵喵&#x27;</span>);

  &#125;
&#125;</code></pre></div>


<h3 id="高阶技巧"><a href="#高阶技巧" class="headerlink" title="高阶技巧"></a>高阶技巧</h3><ol>
<li>定义类的时候, 定义了一个类型;</li>
<li>定义类的时候, 定义了一个构造函数;</li>
</ol>
<div class="hljs"><pre><code class="hljs ts"><span class="hljs-keyword">class</span> Greeter &#123;
  <span class="hljs-keyword">static</span> standardGreeting = <span class="hljs-string">&quot;Hello, there&quot;</span>;  <span class="hljs-comment">// 静态属性</span>
  greeting: <span class="hljs-built_in">string</span>;
  greet() &#123;
      <span class="hljs-keyword">if</span> (<span class="hljs-built_in">this</span>.greeting) &#123;
          <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Hello, &quot;</span> + <span class="hljs-built_in">this</span>.greeting;
      &#125;
      <span class="hljs-keyword">else</span> &#123;
          <span class="hljs-keyword">return</span> Greeter.standardGreeting;
      &#125;
  &#125;
&#125;

<span class="hljs-keyword">let</span> greeter1: Greeter;   <span class="hljs-comment">// 定义类型</span>
greeter1 = <span class="hljs-keyword">new</span> Greeter();   
<span class="hljs-built_in">console</span>.log(greeter1.greet());

<span class="hljs-keyword">let</span> greeterMaker: <span class="hljs-keyword">typeof</span> Greeter = Greeter;    <span class="hljs-comment">// 定义类型</span>
greeterMaker.standardGreeting = <span class="hljs-string">&quot;Hey there!&quot;</span>;  

<span class="hljs-keyword">let</span> greeter2: Greeter = <span class="hljs-keyword">new</span> greeterMaker();  
<span class="hljs-built_in">console</span>.log(greeter2.greet());</code></pre></div>


<ol start="3">
<li>把类当做接口使用</li>
</ol>
<div class="hljs"><pre><code class="hljs ts"><span class="hljs-keyword">class</span> Point &#123;
    x: <span class="hljs-built_in">number</span>;
    y: <span class="hljs-built_in">number</span>;
&#125;

<span class="hljs-keyword">interface</span> Point3d <span class="hljs-keyword">extends</span> Point &#123;
    z: <span class="hljs-built_in">number</span>;
&#125;

<span class="hljs-keyword">let</span> point3d: Point3d = &#123;x: <span class="hljs-number">1</span>, y: <span class="hljs-number">2</span>, z: <span class="hljs-number">3</span>&#125;;</code></pre></div>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                <div class="post-meta mr-3">
                  <i class="iconfont icon-category"></i>
                  
                  <a class="hover-with-bg" href="/categories/typescript/">typescript</a>
                  
                  <a class="hover-with-bg" href="/categories/typescript/JavaScript/">JavaScript</a>
                  
                </div>
                
                
                <div class="post-meta">
                  <i class="iconfont icon-tags"></i>
                  
                  <a class="hover-with-bg" href="/tags/typescript/">typescript</a>
                  
                </div>
                
              </div>
              
              
              <div class="post-prevnext row">
                <article class="post-prev col-6">
                  
                  
                  <a href="/2020/12/29/42.TypeScript%E7%B3%BB%E5%88%974/">
                    <i class="iconfont icon-arrowleft"></i>
                    <span class="hidden-mobile">TypeScript 学习总结之 接口</span>
                    <span class="visible-mobile">上一篇</span>
                  </a>
                  
                </article>
                <article class="post-next col-6">
                  
                  
                  <a href="/2020/12/27/40.TypeScript%E7%B3%BB%E5%88%972/">
                    <span class="hidden-mobile">TypeScript学习总结之函数</span>
                    <span class="visible-mobile">下一篇</span>
                    <i class="iconfont icon-arrowright"></i>
                  </a>
                  
                </article>
              </div>
              
            </div>

            
            <!-- Comments -->
            <article class="comments" id="comments">
              
              
  <div id="vcomments"></div>
  <script type="text/javascript">
    function loadValine() {
      addScript('https://cdn.staticfile.org/valine/1.4.14/Valine.min.js', function () {
        new Valine({
          el: "#vcomments",
          app_id: "hjxsDRXYScicliqD8I8JqoTu-gzGzoHsz",
          app_key: "qUcAEReEqeEVp5wwXJ41uCCp",
          placeholder: "说点什么",
          path: window.location.pathname,
          avatar: "retro",
          meta: ["nick","mail","link"],
          pageSize: "10",
          lang: "zh-CN",
          highlight: true,
          recordIP: false,
          serverURLs: "",
        });
      });
    }
    waitElementVisible('vcomments', loadValine);
  </script>
  <noscript>Please enable JavaScript to view the <a target="_blank" href="https://valine.js.org" rel="nofollow noopener noopener">comments
      powered by Valine.</a></noscript>


            </article>
            
          </article>
        </div>
      </div>
    </div>
    
    <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
      <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div id="tocbot"></div>
</div>

    </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  

  <footer class="mt-5">
  <div class="text-center py-3">
    <div>
      <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a>
      <i class="iconfont icon-love"></i>
      <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener">
        <span>Fluid</span></a>
    </div>
    
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


    

    
  </div>
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/main.js" ></script>

<!-- Plugins -->


  
    
      <script  src="/js/lazyload.js" ></script>
    
  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/js/clipboard-use.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: '#post-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "TypeScript 学习总结之 类&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 70,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "always",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>







  
  
    <script>
      !function (e, t, a) {
        function r() {
          for (var e = 0; e < s.length; e++) s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) : (s[e].y--, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e].scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999");
          requestAnimationFrame(r)
        }

        function n() {
          var t = "function" == typeof e.onclick && e.onclick;
          e.onclick = function (e) {
            t && t(), o(e)
          }
        }

        function o(e) {
          var a = t.createElement("div");
          a.className = "heart", s.push({
            el: a,
            x: e.clientX - 5,
            y: e.clientY - 5,
            scale: 1,
            alpha: 1,
            color: c()
          }), t.body.appendChild(a)
        }

        function i(e) {
          var a = t.createElement("style");
          a.type = "text/css";
          try {
            a.appendChild(t.createTextNode(e))
          } catch (t) {
            a.styleSheet.cssText = e
          }
          t.getElementsByTagName("head")[0].appendChild(a)
        }

        function c() {
          return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")"
        }

        var s = [];
        e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function (e) {
          setTimeout(e, 1e3 / 60)
        }, i(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"), n(), r()
      }(window, document);
    </script>
  











  

  

  

  

  

  





</body>
</html>
